const Service = require('egg').Service;
const OSS = require('ali-oss');

class OssService extends Service {
    constructor(ctx) {
        super(ctx);
        const config = this.app.config.oss.client;
        this.client = new OSS(config);
    }

    async upload(filePath, stream) {
        try {
            return  await this.client.putStream(filePath, stream);
        } catch (error) {
            this.ctx.logger.error('OSS upload error:', error);
            throw error;
        }
    }

    async get(filePath) {
        try {
            const result = await this.client.get(filePath);
            return result;
        } catch (error) {
            this.ctx.logger.error('OSS get error:', error);
            throw error;
        }
    }

    async delete(filePath) {
        try {
            const result = await this.client.delete(filePath);
            return result;
        } catch (error) {
            this.ctx.logger.error('OSS delete error:', error);
            throw error;
        }
    }
}

module.exports = OssService;
